package org.zjx.data.prop;

import org.zjx.command.entity.screen.Npc;
import org.zjx.data.GameSystem;

import java.util.*;

public class ItemNpcData {
    // 重构：以NPC为键，物品列表为值
    public static Map<Npc, List<String>> npcItemMap = new HashMap<>();

    static{
        initNpcMap();
    }

    public static void initNpcMap() {
        npcItemMap.clear();

        // 获取游戏系统实例和世界管理器
        GameSystem gameSystem = GameSystem.getInstance();

        // ========== 长安城物品NPC映射 ==========
        initChangAnItems(gameSystem);
        initJianYeItems(gameSystem);
        initAoLaiGuoItems(gameSystem);
        initChangShouCunItems(gameSystem);

        System.out.println("物品NPC映射初始化完成，共映射了 " + npcItemMap.size() + " 个NPC");
    }

    /**
     * 初始化长安城物品映射
     */
    private static void initChangAnItems(GameSystem gameSystem) {
        // 长安城服装店老板
        Npc changAnClothShopOwner = gameSystem.getWorldManager().findNpc("长安城", "服装店老板");
        List<String> changAnClothItems = Arrays.asList(
                "布衣", "方巾", "马靴", "鳞甲", "五彩裙", "梅花簪子",
                "布裙", "布鞋", "簪子", "面具"
        );
        npcItemMap.put(changAnClothShopOwner, changAnClothItems);

        // 长安城饰品店老板
        Npc changAnAccessoryShopOwner = gameSystem.getWorldManager().findNpc("长安城", "饰品店老板");
        List<String> changAnAccessoryItems = Arrays.asList(
                "银腰带", "珍珠链", "腰带", "护身符"
        );
        npcItemMap.put(changAnAccessoryShopOwner, changAnAccessoryItems);

        // 长安城武器店掌柜 - 基础武器
        Npc changAnWeaponShopManager = gameSystem.getWorldManager().findNpc("长安城", "武器店掌柜");
        List<String> changAnBasicWeaponItems = Arrays.asList(
                "折扇", "红缨枪", "钝铁重剑", "牛皮鞭", "曲柳杖", "铁爪",
                "松木锤", "琉璃珠", "双短剑", "青铜短剑", "柳叶刀", "素纸灯",
                "青铜斧", "五彩缎带", "黄铜圈", "硬木弓", "细木棒", "油纸伞"
        );
        npcItemMap.put(changAnWeaponShopManager, changAnBasicWeaponItems);

        // 长安城武器店老板 - 高级武器
        Npc changAnWeaponShopOwner = gameSystem.getWorldManager().findNpc("长安城", "武器店老板");
        List<String> changAnAdvancedWeaponItems = Arrays.asList(
                "离情环", "珍宝珠", "龙凤双剑", "白椴椴杖", "紫竹伞", "赭赭石巨剑",
                "玉如意", "幽冥鬼爪", "乌龙鞭", "锯齿矛", "双面斧", "精钢扇",
                "夜魔弯刀", "八棱金瓜", "红灯笼", "吴越剑", "紫檀弓", "金丝彩带"
        );
        npcItemMap.put(changAnWeaponShopOwner, changAnAdvancedWeaponItems);

        // 长安城罗道人
        Npc changAnLuoDaoRen = gameSystem.getWorldManager().findNpc("长安城", "罗道人");
        List<String> changAnLuoDaoRenItems = Arrays.asList(
                "苍魂珠", "骷髅吊坠", "侠客履", "飞行符", "玉树腰带", "水晶腰带"
        );
        npcItemMap.put(changAnLuoDaoRen, changAnLuoDaoRenItems);

        // 长安城药店老板
        Npc changAnDrugShopOwner = gameSystem.getWorldManager().findNpc("长安城", "药店老板");
        List<String> changAnDrugItems = Arrays.asList(
                "香叶", "百色花", "草果", "鬼切草", "佛手", "山药",
                "月见草", "九香虫", "七叶莲"
        );
        npcItemMap.put(changAnDrugShopOwner, changAnDrugItems);

        // 长安城杂货店老板
        Npc changAnGroceryShopOwner = gameSystem.getWorldManager().findNpc("长安城", "杂货店老板");
        List<String> changAnGroceryItems = Arrays.asList(
                "洞冥草", "桃花", "摄妖香", "兰花", "超级宠物口粮", "宠物口粮"
        );
        npcItemMap.put(changAnGroceryShopOwner, changAnGroceryItems);

        // 长安城福寿店老板
        Npc changAnFuShouShopOwner = gameSystem.getWorldManager().findNpc("长安城", "福寿店老板");
        List<String> changAnFuShouItems = Arrays.asList(
                "香", "烛", "黄纸"
        );
        npcItemMap.put(changAnFuShouShopOwner, changAnFuShouItems);

        // 长安城酒楼老板
        Npc changAnRestaurantOwner = gameSystem.getWorldManager().findNpc("长安城", "酒楼老板");
        List<String> changAnRestaurantItems = Collections.singletonList("包子");
        npcItemMap.put(changAnRestaurantOwner, changAnRestaurantItems);

        // 长安城乐器店老板
        Npc changAnMusicShopOwner = gameSystem.getWorldManager().findNpc("长安城", "乐器店老板");
        List<String> changAnMusicItems = Arrays.asList("箫", "钹钹");
        npcItemMap.put(changAnMusicShopOwner, changAnMusicItems);
    }

    /**
     * 初始化建邺城物品映射
     */
    private static void initJianYeItems(GameSystem gameSystem) {
        // 建邺城武器店掌柜 - 基础武器
        Npc jianYeWeaponShopManager = gameSystem.getWorldManager().findNpc("建邺城", "武器店掌柜");
        List<String> jianYeBasicWeaponItems = Arrays.asList(
                "折扇", "红缨枪", "钝铁重剑", "牛皮鞭", "曲柳杖", "铁爪",
                "松极锤", "琉璃珠", "双短剑", "青铜短剑", "柳叶刀", "素纸灯",
                "青铜斧", "五彩缎带", "黄铜圈", "硬木弓", "细木棒", "油纸伞"
        );
        npcItemMap.put(jianYeWeaponShopManager, jianYeBasicWeaponItems);

        // 建邺城武器店老板 - 高级武器
        Npc jianYeWeaponShopOwner = gameSystem.getWorldManager().findNpc("建邺城", "武器店老板");
        List<String> jianYeAdvancedWeaponItems = Arrays.asList(
                "桃印铁刃", "幻彩银纱", "红木杖", "镔镔铁双剑", "牛筋鞭", "竹骨灯",
                "曲尖枪", "金丝魔棒", "铁骨扇", "天狼爪", "开山斧", "红罗伞",
                "铁齿剑", "苗刀", "镔镔铁锤", "精钢日月圈", "铁胆弓", "水晶珠"
        );
        npcItemMap.put(jianYeWeaponShopOwner, jianYeAdvancedWeaponItems);

        // 建邺城服装店老板
        Npc jianYeClothShopOwner = gameSystem.getWorldManager().findNpc("建邺城", "服装店老板");
        List<String> jianYeClothItems = Arrays.asList(
                "缎带", "五色飞石", "布衣", "方巾", "腰带", "布帽",
                "布裙", "皮衣", "布鞋", "簪子", "丝绸长裙", "护身符",
                "牛皮靴", "玉钗"
        );
        npcItemMap.put(jianYeClothShopOwner, jianYeClothItems);

        // 建邺城药店老板
        Npc jianYeDrugShopOwner = gameSystem.getWorldManager().findNpc("建邺城", "药店老板");
        List<String> jianYeDrugItems = Arrays.asList(
                "熊胆", "紫丹罗", "金创药", "麝香", "四叶花", "佛光舍利子",
                "山药", "丁香水", "血色茶花"
        );
        npcItemMap.put(jianYeDrugShopOwner, jianYeDrugItems);

    }

    /**
     * 初始化傲来国物品映射
     */
    private static void initAoLaiGuoItems(GameSystem gameSystem) {
        // 傲来国牛师傅 - 服饰店
        Npc aoLaiGuoNiuShiFu = gameSystem.getWorldManager().findNpc("傲来国", "牛师傅");
        List<String> aoLaiGuoClothItems = Arrays.asList(
                "纶巾", "珍珠头带", "水晶腰带", "侠客履", "龙鳞羽衣", "玉树腰带",
                "锁子甲", "骷髅吊坠", "苍魂珠"
        );
        npcItemMap.put(aoLaiGuoNiuShiFu, aoLaiGuoClothItems);

        // 傲来国沈妙衣 - 药店
        Npc aoLaiGuoShenMiaoYi = gameSystem.getWorldManager().findNpc("傲来国", "沈妙衣");
        List<String> aoLaiGuoDrugItems = Arrays.asList(
                "白玉骨头", "天青地白", "龙须草", "八角莲叶"
        );
        npcItemMap.put(aoLaiGuoShenMiaoYi, aoLaiGuoDrugItems);

        // 傲来国杜天 - 武器店
        Npc aoLaiGuoDuTian = gameSystem.getWorldManager().findNpc("傲来国", "杜天");
        List<String> aoLaiGuoWeaponItems = Arrays.asList(
                "无极丝", "锦绣帷", "墨铁拐", "铁面扇", "金刺轮", "青龙牙",
                "点金棒", "竹节双极", "双弦钺钺", "鲤鱼灯", "金背大砍刀", "青锋剑",
                "狼牙锤", "钢结鞭", "璧玉长铗铗", "乌金三叉戟", "翡翠珠", "宝雕长弓"
        );
        npcItemMap.put(aoLaiGuoDuTian, aoLaiGuoWeaponItems);
    }

    /**
     * 初始化长寿村物品映射
     */
    private static void initChangShouCunItems(GameSystem gameSystem) {
        // 长寿村药店老板
        Npc changShouCunDrugShopOwner = gameSystem.getWorldManager().findNpc("长寿村", "药店老板");
        List<String> changShouCunDrugItems = Arrays.asList(
                "旋复花", "灵脂", "曼陀罗花", "水黄莲", "人参"
        );
        npcItemMap.put(changShouCunDrugShopOwner, changShouCunDrugItems);

        // 长寿村裁缝张 - 服饰店
        Npc changShouCunCaiFengZhang = gameSystem.getWorldManager().findNpc("长寿村", "裁缝张");
        List<String> changShouCunClothItems = Arrays.asList(
                "九宫坠", "白面狼牙", "江湖夜雨", "风头钗", "络缨丝带", "神行靴",
                "琥珀腰链", "紧身衣", "天香披肩"
        );
        npcItemMap.put(changShouCunCaiFengZhang, changShouCunClothItems);

        // 长寿村武器店老板
        Npc changShouCunWeaponShopOwner = gameSystem.getWorldManager().findNpc("长寿村", "武器店老板");
        List<String> changShouCunWeaponItems = Arrays.asList(
                "勾魂爪", "天蚕丝带", "风火圈", "錾錾金宝弓", "精钢禅钺钺", "狼牙双剑",
                "火焰枪", "龙泉剑", "莲华珠", "青铜古剑", "烈焰锤", "芙蓉花灯",
                "幽兰帐", "雁翅刀", "百折扇", "蛇骨鞭", "云龙棒", "玄铁牛角杖"
        );
        npcItemMap.put(changShouCunWeaponShopOwner, changShouCunWeaponItems);
    }


    /**
     * 根据物品名称查找对应的NPC
     * 如果多个NPC售卖同一物品，返回第一个找到的NPC
     */
    public static Npc findNpcByItem(String itemName) {
        for (Map.Entry<Npc, List<String>> entry : npcItemMap.entrySet()) {
            if (entry.getValue().contains(itemName)) {
                return entry.getKey();
            }
        }
        return null;
    }

    /**
     * 根据物品名称查找所有售卖该物品的NPC
     * 返回所有售卖该物品的NPC列表
     */
    public static List<Npc> findAllNpcsByItem(String itemName) {
        List<Npc> result = new ArrayList<>();
        for (Map.Entry<Npc, List<String>> entry : npcItemMap.entrySet()) {
            if (entry.getValue().contains(itemName)) {
                result.add(entry.getKey());
            }
        }
        return result;
    }

    /**
     * 根据物品名称和场景名称查找NPC
     * 在指定场景中查找售卖该物品的NPC
     */
    public static Npc findNpcByItemInScene(String itemName, String sceneName) {
        for (Map.Entry<Npc, List<String>> entry : npcItemMap.entrySet()) {
            if (entry.getKey().getSceneName().equals(sceneName) && entry.getValue().contains(itemName)) {
                return entry.getKey();
            }
        }
        return null;
    }

    /**
     * 根据物品名称和场景名称查找所有售卖该物品的NPC
     */
    public static List<Npc> findAllNpcsByItemInScene(String itemName, String sceneName) {
        List<Npc> result = new ArrayList<>();
        for (Map.Entry<Npc, List<String>> entry : npcItemMap.entrySet()) {
            if (entry.getKey().getSceneName().equals(sceneName) && entry.getValue().contains(itemName)) {
                result.add(entry.getKey());
            }
        }
        return result;
    }

    /**
     * 根据物品名称模糊查找NPC
     * 支持物品名称的部分匹配
     */
    public static List<Npc> findNpcsByItemFuzzy(String itemNamePart) {
        List<Npc> result = new ArrayList<>();
        for (Map.Entry<Npc, List<String>> entry : npcItemMap.entrySet()) {
            for (String item : entry.getValue()) {
                if (item.contains(itemNamePart)) {
                    result.add(entry.getKey());
                    break; // 同一个NPC只需要添加一次
                }
            }
        }
        return result;
    }


    /**
     * 获取NPC售卖的所有物品
     */
    public static List<String> getItemsByNpc(Npc npc) {
        return npcItemMap.getOrDefault(npc, new ArrayList<>());
    }

    /**
     * 获取NPC售卖的所有物品（通过场景和NPC名称）
     */
    public static List<String> getItemsByNpc(String sceneName, String npcName) {
        for (Map.Entry<Npc, List<String>> entry : npcItemMap.entrySet()) {
            if (entry.getKey().getSceneName().equals(sceneName) && entry.getKey().getName().equals(npcName)) {
                return entry.getValue();
            }
        }
        return new ArrayList<>();
    }

    /**
     * 检查NPC是否售卖指定物品
     */
    public static boolean doesNpcSellItem(Npc npc, String itemName) {
        List<String> items = npcItemMap.get(npc);
        return items != null && items.contains(itemName);
    }

    /**
     * 检查指定场景的NPC是否售卖物品
     */
    public static boolean doesNpcSellItemInScene(String sceneName, String npcName, String itemName) {
        for (Map.Entry<Npc, List<String>> entry : npcItemMap.entrySet()) {
            Npc npc = entry.getKey();
            if (npc.getSceneName().equals(sceneName) && npc.getName().equals(npcName)) {
                return entry.getValue().contains(itemName);
            }
        }
        return false;
    }

    /**
     * 获取所有售卖物品的NPC列表
     */
    public static List<Npc> getAllItemNpcs() {
        return new ArrayList<>(npcItemMap.keySet());
    }

    /**
     * 获取指定场景中所有售卖物品的NPC
     */
    public static List<Npc> getItemNpcsInScene(String sceneName) {
        List<Npc> result = new ArrayList<>();
        for (Npc npc : npcItemMap.keySet()) {
            if (npc.getSceneName().equals(sceneName)) {
                result.add(npc);
            }
        }
        return result;
    }

    /**
     * 获取所有可购买的物品列表
     */
    public static List<String> getAllPurchasableItems() {
        List<String> result = new ArrayList<>();
        for (List<String> items : npcItemMap.values()) {
            result.addAll(items);
        }
        return result;
    }

    /**
     * 获取指定场景中所有可购买的物品
     */
    public static List<String> getPurchasableItemsInScene(String sceneName) {
        List<String> result = new ArrayList<>();
        for (Map.Entry<Npc, List<String>> entry : npcItemMap.entrySet()) {
            if (entry.getKey().getSceneName().equals(sceneName)) {
                result.addAll(entry.getValue());
            }
        }
        return result;
    }

    /**
     * 添加物品到NPC的售卖列表
     */
    public static void addItemToNpc(Npc npc, String itemName) {
        List<String> items = npcItemMap.computeIfAbsent(npc, k -> new ArrayList<>());
        if (!items.contains(itemName)) {
            items.add(itemName);
        }
    }

    /**
     * 从NPC的售卖列表中移除物品
     */
    public static void removeItemFromNpc(Npc npc, String itemName) {
        List<String> items = npcItemMap.get(npc);
        if (items != null) {
            items.remove(itemName);
            if (items.isEmpty()) {
                npcItemMap.remove(npc);
            }
        }
    }

    /**
     * 批量添加物品到NPC
     */
    public static void addItemsToNpc(Npc npc, List<String> itemNames) {
        List<String> items = npcItemMap.computeIfAbsent(npc, k -> new ArrayList<>());
        for (String itemName : itemNames) {
            if (!items.contains(itemName)) {
                items.add(itemName);
            }
        }
    }

    /**
     * 清空NPC的售卖物品列表
     */
    public static void clearNpcItems(Npc npc) {
        npcItemMap.remove(npc);
    }

}